from typing import List
from app.models.models import SysMenu
from app.schemas.schemas import MenuTreeNode

def build_menu_tree(menu_items: List[SysMenu], parent_id: int = 0) -> List[MenuTreeNode]:
    """
    构建菜单树结构
    :param menu_items: 菜单列表
    :param parent_id: 父节点ID
    :return: 菜单树列表
    """
    tree = []
    for item in menu_items:
        if item.pid == parent_id:
            node = MenuTreeNode(
                id=item.id,
                pid=item.pid,
                name=item.name,
                code=item.code,
                path=item.path,
                icon=item.icon,
                is_active=item.is_active,
                children=build_menu_tree(menu_items, item.id)
            )
            tree.append(node)
    return tree 